<?php


namespace app\common\model;


use think\model\concern\SoftDelete;

class ProductsType extends Base
{

    // 软删除
    use SoftDelete;
    protected $deleteTime = 'is_del';
    protected $defaultSoftDelete = 1;

    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_products_type';
    // 主键ID
    protected $pk = 'type_id';

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    function get_list($field='*',$cond=null, $orderby='sort asc')
    {
		$where = null;

        if(isset($cond['shop_id'])){
            $where[] = ['shop_id','=', $cond['shop_id']];
        }

        if(isset($cond['pid'])){
            $where[] = ['p_id','=', $cond['pid']];
        }

		if(!empty($cond['type'])){
			$where[] = ['type','=', $cond['type']];
		}

		if(!empty($cond['type_name'])){
			$where[] = ['type_name','LIKE', "%{$cond['type_name']}%"];
		}

        $rs = $this
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->select();
        return $rs;
    }

    // 获取分页列表
    function get_list_page($field,$cond,$limit, $orderby='sort asc')
    {
        $where = null;

		if(!empty($cond['type_name'])){
			$where[] = ['type_name','LIKE', "%{$cond['type_name']}%"];
		}

        $rs = $this->field($field)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }

	//获取树形数据
	function get_tree($field = 'type_id,p_id,type_name,sort,add_time',$cond = ['is_del' => 1], $orderby='p_id asc,sort asc',$type = 'online')
	{
		$where[] = ['type','=',$type];

        if(!empty($cond['is_del'])){
            $where[] = ['is_del','=', $cond['is_del']];
        }
		if(!empty($cond['type_name'])){
			$where[] = ['type_name','LIKE', "%{$cond['type_name']}%"];
		}
		$rs = $this
			->field($field)
			->where($where)
			->order($orderby)
			->select()
			->toArray();
		if(!$rs){
			return [];
		}

		foreach ($rs as $key => $value){
			if($value['p_id'] === 0){
				$fdata[$value['type_id']] = $value;
				$fdata[$value['type_id']]['children'] = [];
			}else{
				$fdata[$value['p_id']]['children'][] = $value;
			}
		}

		return array_values($fdata);
	}

    //获取只看分类树形数据
	function get_show_tree(){

        $sql = "SELECT `type_id`,`p_id`,`type_name` FROM`ddt_products_type` WHERE `is_del` = 1 AND `type` = 'offline' AND `is_show` = 2  OR `p_id` IN (select type_id from ddt_products_type b where b.is_del = 1 and b.type = 'offline' AND `is_show` = 2) ORDER BY `p_id` ASC,`sort` ASC";
        $rs = $this->query($sql);
        if(!$rs){
            return [];
        }
        foreach ($rs as $key => $value){
            if($value['p_id'] === 0){
                $fdata[$value['type_id']] = $value;
                $fdata[$value['type_id']]['children'] = [];
            }else{
                $fdata[$value['p_id']]['children'][] = $value;
            }
        }

        return array_values($fdata);
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['type_id' => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->type_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

//   通过子级获取上级是否为展示分类
    public function checkshowtype($type_id){
        $rs = $this
            ->alias('t')
            ->field('t.*,(select is_show from ddt_products_type pt where pt.type_id = t.p_id) as can_show')
            ->where('type_id','=',$type_id)
            ->find();
        return $rs;
    }


	public function prod()
	{
		return $this->hasMany('Products','type');
	}

	// 关联子菜单
	public function subs()
	{
		return $this->hasMany('ProductsType','p_id');
	}

    function Del($id)
    {
		// 软删除 父级+子级一起删除  （十一）
		$article = ProductsType::get($id,'subs');
		return $article->together('subs')->delete();

    }
}